//
//  AppDelegate.m
//  Audit
//
//  Created by cc on 15-2-3.
//  Copyright (c) 2015年 cc. All rights reserved.
//

#import "AppDelegate.h"
#import "LoginViewController.h"
#import "SMTabbedSplitViewController.h"
#import "SMTabBarItem.h"
#import "QueryViewController.h"
#import "EnteryViewController.h"




@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
//    self.showGuide = YES;
//    self.showFlash = YES;
    
    
    NSString *value = [[NSUserDefaults standardUserDefaults] objectForKey:SHOW_GUIDE];
    if ([value isEqualToString:@"0"]) {
        self.showGuide = NO;
        self.showFlash = NO;

    }
    else
    {
        self.showGuide = YES;
        self.showFlash = YES;
    }
    
//
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    self.window.backgroundColor = [UIColor whiteColor];
    
    LoginViewController *loginVC = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
    
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:loginVC];
    navi.navigationBarHidden = YES;
    self.window.rootViewController = navi;
    [self.window makeKeyAndVisible];
    
    gAlert = [[HFAlert alloc] init];
    
    return YES;
}

-(void)showMainView
{
    SMTabbedSplitViewController *split = [[SMTabbedSplitViewController alloc] init];
    
    QueryViewController *tab1VC = [[QueryViewController alloc] init];
    tab1VC.view.backgroundColor = [UIColor colorWithRed:0/255.0 green:127/255.0 blue:237/255.0 alpha:1.0];
    SMTabBarItem *tab1 = [[SMTabBarItem alloc] initWithVC:tab1VC image:[UIImage imageNamed:@"query_nomal.png"] andTitle:nil];
    tab1.selectedImage = [UIImage imageNamed:@"query_select.png"];
    
    
    EnteryViewController *tab2VC = [[EnteryViewController alloc] init];
    tab2VC.view.backgroundColor = [UIColor colorWithRed:59/255.0 green:89/255.0 blue:152/255.0 alpha:1.0];
    SMTabBarItem *tab2 = [[SMTabBarItem alloc] initWithVC:tab2VC image:[UIImage imageNamed:@"add_nomal.png"] andTitle:nil];
    tab2.selectedImage = [UIImage imageNamed:@"add_select.png"];
    
    
    SMTabBarItem *action = [[SMTabBarItem alloc] init];
    SMTabBarItem *action2 = [[SMTabBarItem alloc] init];
    split.tabsViewControllers = @[tab1, tab2];
    split.actionsButtons = @[action, action2];
    
    split.background = [UIColor whiteColor];
    
    self.window.rootViewController = split;
    [self.window makeKeyAndVisible];
    
    
    
}

-(void)showLogin
{
    LoginViewController *loginVC = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
    
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:loginVC];
    navi.navigationBarHidden = YES;
    self.window.rootViewController = navi;
    [self.window makeKeyAndVisible];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    
    self.showGuide = NO;
    self.showFlash = NO;
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    self.showGuide = NO;
    self.showFlash = NO;
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


-(BOOL)showGuide
{
    NSString *value = [[NSUserDefaults standardUserDefaults] objectForKey:SHOW_GUIDE];
    if ([value isEqualToString:@"1"]) {
        return YES;
    }
    return NO;
}

-(void)setShowGuide:(BOOL)showGuide
{
    if (showGuide) {
        [[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:SHOW_GUIDE];
    }else{
        [[NSUserDefaults standardUserDefaults] setValue:@"0" forKey:SHOW_GUIDE];
    }
}
@end








// UIViewController
@implementation UIViewController (Rotation_IOS6)

// IOS5默认支持竖屏
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

// IOS6默认不开启旋转，如果subclass需要支持屏幕旋转，重写这个方法return YES即可
- (BOOL)shouldAutorotate {
    return NO;
}

// IOS6默认支持竖屏
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

@end


// UINavigationController
@implementation UINavigationController (Rotation_IOS6)

- (BOOL)shouldAutorotate {
    return [[self.viewControllers lastObject] shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations {
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

@end


// UITabBarController
@implementation UITabBarController (Rotation_IOS6)

- (BOOL)shouldAutorotate {
    return [[self.viewControllers lastObject] shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations {
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

@end
